extends Spatial

export var initV = 10;#初始速度
export var fG = Vector3(0,-9.8,0)#重力加速度

export var projectile_step = 0.1#曲线采样间隔，值越小运算量约大，但曲线约精细
export var projectile_time = 3.0#投射物抛物时间，代表了投射物抛射的最长运动时间

func _process(delta):
	var res = ProjectileRaycast()
	$"../Painter".clear()
	$"../Painter".Lines(res.points)
	$"../LandMark".visible = res.hit
	if res.hit:
		$"../LandMark".transform.origin = res.point

func ProjectileRaycast():#投射投影返回 是否hit，hit的位置，抛物线的采样点（用于绘制投射曲线）
	#Projectile
	var result = {
		"hit":false,
		"point":null,
		"points":[]
	}
	var v = $Projectile.global_transform.basis*Vector3.FORWARD*initV
	var p = $Projectile.global_transform.origin

	var space_state = get_world().direct_space_state
	var t = 0
	result.points.append(p)
	while t<projectile_time:
		var prePos = p
		p += v * projectile_step
		v += fG * projectile_step
		var res = space_state.intersect_ray(prePos,p,[self])
		if !res.empty():
			result.hit = true
			result.point = res.position
			result.points.append(res.position)
			return result
		result.points.append(p)
		t+= projectile_step
	return result
